package com.cysd.ewmxns.sdw_ui.dialog;

import android.animation.AnimatorSet;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;



import com.hlyj.http.ad.NewGroMoreFeedAdManager;
import com.hnyyac.ad.mds.utils.BaseAdConstants;
import qrrspeeelild.rqrc0ode.andro0ild.R;


public class FunctionGuideDialog extends BaseDialog{
    private RelativeLayout rv_function_guide;
    private ImageView img_close;
    private FrameLayout frame_content;
    public   onGuideDialogListener listener;
    private Context mContext;

    private NewGroMoreFeedAdManager newGromoreFeedAd;

    public FunctionGuideDialog(@NonNull Context context) {
        super(context);
        this.mContext=context;
    }

    @Override
    protected float setWidthScale() {
        return 1.0f;
    }

    @Override
    protected AnimatorSet setEnterAnim() {
        return null;
    }

    @Override
    protected AnimatorSet setExitAnim() {
        return null;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void init() {
        frame_content=findViewById(R.id.frame_content);


        initAdAfterLayoutMeasured();
        frame_content=findViewById(R.id.frame_content);
        rv_function_guide=findViewById(R.id.rv_guide);
        img_close=findViewById(R.id.iv_close);


        rv_function_guide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.btnOnclick();
            }
        });

        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.btnOnclose();
            }
        });

    }
    /**
     * 在 Dialog 布局完成后初始化广告，并监听高度变化
     */
    private void initAdAfterLayoutMeasured() {
        final View decorView = getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 移除监听，避免重复调用
                decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                // 获取 Dialog 的实际宽度
                int dialogWidth = decorView.getWidth();
                Log.d("FunctionGuideDialog", "Dialog宽度: " + dialogWidth);

                // 初始化广告（确保宽度有效）
                if (mContext instanceof Activity) {
                    newGromoreFeedAd = new NewGroMoreFeedAdManager(
                            (Activity) mContext,
                            BaseAdConstants.FEEDAD_Id,
                            frame_content,
                            3
                    );

                    // 设置广告宽度（减去左右 margin 10dp）
                    int adWidth = frame_content.getWidth();
                    int adHeight = 350; // 默认高度（可根据需求调整）
                    newGromoreFeedAd.setFeedAdWidth(adWidth, adHeight);
                    // 加载广告
                    newGromoreFeedAd.loadAd();

                    // 监听广告容器高度变化
                    setupAdHeightListener();
                } else {
                    Log.e("FunctionGuideDialog", "必须使用 Activity 的 Context");
                }
            }
        });
    }

    /**
     * 监听广告容器高度变化，动态调整 Dialog 高度
     */
    private void setupAdHeightListener() {
        frame_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int adHeight = frame_content.getHeight();
                if (adHeight > 0) {
                    Log.d("" +
                            "", "广告高度变化: " + adHeight);

                    // 动态更新 Dialog 的 Window 高度
                    Window window = getWindow();
                    if (window != null) {
                        WindowManager.LayoutParams params = window.getAttributes();
                        params.height = ViewGroup.LayoutParams.WRAP_CONTENT; // 或固定值
                        window.setAttributes(params);
                    }
                }
            }
        });
    }
    @Override
    protected int getContentViewId() {
        return R.layout.dialog_function_guide;
    }

    public void setOnGuideDialogListener(onGuideDialogListener listener){
        this.listener=listener;
    }

   public interface onGuideDialogListener{
        void btnOnclick();
        void btnOnclose();
    }
}
